package main

import (
	"fmt"
	"sort"
)

type Student struct {
	name  string
	score int
}

func main() {
	m := map[string]int{
		"tom":   60,
		"ben":   70,
		"sam":   80,
		"jerry": 90,
	}
	var keys []string
	for k := range m {
		keys = append(keys, k)
	}
	fmt.Println(keys)
	sort.Strings(keys)
	fmt.Println(keys)

	for _, k := range keys {
		fmt.Println(k, m[k])
	}
	fmt.Println("-----------------")
	// value排序
	type Entry struct {
		k string
		v int
	}
	var entries []Entry
	for k, v := range m {
		entries = append(entries, Entry{k, v})
	}
	fmt.Println(entries)
	sort.Slice(entries, func(i, j int) bool {
		return entries[i].v < entries[j].v // 升序
		// return entries[i].v > entries[j].v // 降序
	})
	fmt.Println(entries)
}
